package com.lenovo.nova.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.text.Editable;

import com.lenovo.nova.util.collection.Collections;
import com.lenovo.nova.util.collection.Collections.OnShowCollectionHelper;
import com.lenovo.nova.util.debug.MyOldLog;
import com.lenovo.nova.util.debug.ObjectUtil;
import com.lenovo.nova.util.debug.slog;

/**
 * 字符串工具类
 * @author liuzhd
 */
public class StringUtil {

	public static final String str_current_path = System.getProperty("user.dir");
	public static final String str_new_line = System.getProperty("line.separator");
	
	/**
	 * 判断字符串是不是空的
	 * @param paramString
	 * @return 如果是true的话说明是空的，如果是false的话就不是空的
	 */
	public static boolean isStringEmpty(CharSequence paramString) {
		if ((paramString != null) && 
				(paramString.toString().trim().length() != 0) &&
				!paramString.toString().trim().equals("")
				) {
			return false;
		}
		return true;
	}
	
	public static boolean isStringEmpty(Editable paramString) {
		if(paramString == null){
			return true;
		}
		return isStringEmpty(paramString.toString());
	}
	
	public static <T> List<T> arrayToList(T[] array){
		ArrayList<T> list = new ArrayList<T>();
		for (T t : array) {
			list.add(t);
		}
		return list;
	}
	
	public static void showArrayValue(Object ...objs){
		if(objs != null){
			for(int i = 0 ;i < objs.length; i++){
				slog.p("index " + i + " " + objs[i]);
			}
		}else{
			MyOldLog.e("objs is null");
		}
	}
	
	public static String showMappingValue(Map map){
		Set keys = map.keySet();
		System.out.println("key = value");
		for (Object key : keys) {
			new ObjectUtil("=", key + "=" + map.get(key));
		}
		String mappingInfoValue = ObjectUtil.formatPrint();
		System.out.println(mappingInfoValue);
		return mappingInfoValue;
	}
	
	public static <E> String showCollection(Collection<E> collection,OnShowCollectionHelper<E> helper){
		return Collections.showCollection(collection, helper);
	}
	
	public static String getFileSuffix(String filePath){
		int lastDotIndex = filePath.lastIndexOf(".");
		MyOldLog.p("lastDotIndex is " + lastDotIndex);
		
		String subStr = filePath.substring(lastDotIndex + 1);
		if(subStr != null){
			subStr = subStr.trim().toLowerCase();
		}
		return subStr;
	}
	
	
	public static boolean isNumber(String str) {
		boolean isNumber = true;
		try {
			Integer.parseInt(str);
		} catch (NumberFormatException e) {
			isNumber = false;
		}
		return isNumber;
	}
	
	public static String formatShortSize(Context context,long number){
		return android.text.format.Formatter.formatShortFileSize(context, number);
	}
	

	/**
	 *把横向排列的文字变成垂直排列的文字
	 * @param str
	 * @return
	 */
	public static String getVerticalText(String str){
		StringBuilder sb = new StringBuilder();
		for(int i = 0;i < str.length(); i++){
			sb.append(str.charAt(i));
			sb.append(str_new_line);
		}
		return sb.toString();
	}
	
	
	/**
	 * 
	 * @param url
	 * @return
	 */
	public static String hashCode(String url) {
		int res = url.hashCode();
		if (res > 0) {
			return String.valueOf(res);
		} else {
			long result = 0;
			result = (long) (res >>> 24 & 0xff) << 24;
			result |= (long) (res >>> 16 & 0xff) << 16;
			result |= (long) (res >>> 8 & 0xff) << 8;
			result |= (long) (res & 0xff);
			return String.valueOf(result);
		}
	}
}
